package com.zjuee.web.ledger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.constant.DictionaryConstants;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.exception.ServiceException;
import com.zjuee.base.model.common.company.Company;
import com.zjuee.base.utils.DateUtils;
import com.zjuee.base.dto.common.ledger.LedgerDangerSubtotalDto;
import com.zjuee.base.model.common.ledger.LedgerDangerSubtotal;
import com.zjuee.base.model.common.ledger.LedgerDispose;
import com.zjuee.base.model.common.ledger.LedgerProduce;
import com.zjuee.service.LedgerDangerSubtotalService;
import com.zjuee.service.company.service.CompanyService;
import com.zjuee.web.ledger.mapper.LedgerDangerSubtotalMapper;
import com.zjuee.web.ledger.mapper.LedgerProduceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@Service
public class LedgerDangerSubtotalServiceImpl implements LedgerDangerSubtotalService {

    @Resource
    CompanyService companyService;
    @Resource
    LoginDto loginDto;
    @Resource
    LedgerProduceMapper ledgerProduceMapper;
    @Resource
    LedgerDangerSubtotalMapper ledgerDangerSubtotalMapper;
    @Override
    public Long add(LedgerDangerSubtotal type) {
        return null;
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        return false;
    }

    @Override
    public boolean update(LedgerDangerSubtotal type) {
        return false;
    }

    @Override
    public LedgerDangerSubtotal selectById(Long id, Long validateId) {
        return null;
    }

    @Override
    public Page<LedgerDangerSubtotal> list(LedgerDangerSubtotalDto dto) {
        return null;
    }

    @Override
    public LambdaQueryWrapper<LedgerDangerSubtotal> fuzzyQuery(LedgerDangerSubtotalDto dto) {
        return null;
    }

    @Override
    public List<LedgerDangerSubtotal> selectList(LedgerDangerSubtotal ledgerDangerSubtotal) {
        return null;
    }

    @Override
    public void ledgerDangerProduceReport(LedgerProduce ledgerProduce, String state) {
        // 需满足为危废或医废
        if (DictionaryConstants.TYPE_DANGER.equals(ledgerProduce.getWlpType())
                || DictionaryConstants.TYPE_MEDICAL.equals(ledgerProduce.getWlpType())) {
            LedgerDangerSubtotal ledgerDangerSubtotal = new LedgerDangerSubtotal();
            this.prepareProduceCondition(ledgerDangerSubtotal, ledgerProduce);
            // 类型：产生 -> 1
            ledgerDangerSubtotal.setVldsType(1);

            if (DictionaryConstants.VL_LEDGER_NOMAL.equals(state)) {
                // 正常增加
                ledgerDangerSubAppend(ledgerProduce, ledgerDangerSubtotal);
            } else if (DictionaryConstants.VL_LEDGER_DELETE.equals(state)) {
                // 撤销扣除
                ledgerDangerSubRemove(ledgerProduce, ledgerDangerSubtotal);
            } else if (DictionaryConstants.VL_LEDGER_CHANGE.equals(state)) {
                // 数量更改
                ledgerDangerSubChange(ledgerProduce, ledgerDangerSubtotal);
            }
        }
    }

    @Override
    public void ledgerDangerDisposeReport(LedgerDispose ledgerDispose, String state)  {

        // 需满足为危废或医废
        if (DictionaryConstants.TYPE_DANGER.equals(ledgerDispose.getWldType())
                || DictionaryConstants.TYPE_MEDICAL.equals(ledgerDispose.getWldType())) {
            LedgerDangerSubtotal ledgerDangerSubtotal = new LedgerDangerSubtotal();
            this.prepareDisposeCondition(ledgerDangerSubtotal, ledgerDispose);
            // 类型：处置 -> 2
            ledgerDangerSubtotal.setVldsType(2);

            if (DictionaryConstants.VL_LEDGER_NOMAL.equals(state)) {
                // 正常增加
                this.ledgerDangerSubAppend(ledgerDispose, ledgerDangerSubtotal);
            } else if (DictionaryConstants.VL_LEDGER_DELETE.equals(state)) {
                // 撤销扣除
                ledgerDangerSubRemove(ledgerDispose, ledgerDangerSubtotal);
            } else if (DictionaryConstants.VL_LEDGER_CHANGE.equals(state)) {
                // 数量更改
                ledgerDangerSubChange(ledgerDispose, ledgerDangerSubtotal);
            }
        }
    }
    private LedgerDangerSubtotal prepareProduceCondition(LedgerDangerSubtotal ledgerDangerSubtotal, LedgerProduce ledgerProduce) {
        // 获取产生企业信息
        Company company = companyService.selectById(loginDto.getOrgBusinessId(),null).getCompany();
//        Company company = companyClient.selectById(loginDto.getOrgBusinessId()).getData().getCompany();
        ledgerDangerSubtotal.setVldsProvince(company.getBciProvince());
        ledgerDangerSubtotal.setVldsCity(company.getBciCity());
        ledgerDangerSubtotal.setVldsArea(company.getBciArea());
        ledgerDangerSubtotal.setVldsStreet(company.getBciStreet());
        ledgerDangerSubtotal.setVldsDangerType(ledgerProduce.getWlpType());
        ledgerDangerSubtotal.setVldsBcode(ledgerProduce.getWlpBcode());
        ledgerDangerSubtotal.setVldsScode(ledgerProduce.getWlpScode());

        return ledgerDangerSubtotal;
    }

    private LedgerDangerSubtotal prepareDisposeCondition(LedgerDangerSubtotal ledgerDangerSubtotal, LedgerDispose ledgerDispose) {
        // 获取处置企业信息
        Company company = companyService.selectById(loginDto.getOrgBusinessId(),null).getCompany();
//        Company company = companyClient.selectById(loginDto.getOrgBusinessId()).getData().getCompany();
        ledgerDangerSubtotal.setVldsProvince(company.getBciProvince());
        ledgerDangerSubtotal.setVldsCity(company.getBciCity());
        ledgerDangerSubtotal.setVldsArea(company.getBciArea());
        ledgerDangerSubtotal.setVldsStreet(company.getBciStreet());
        ledgerDangerSubtotal.setVldsDangerType(ledgerDispose.getWldType());
        ledgerDangerSubtotal.setVldsBcode(ledgerDispose.getWldBcode());
        ledgerDangerSubtotal.setVldsScode(ledgerDispose.getWldScode());

        return ledgerDangerSubtotal;
    }
    private void ledgerDangerSubAppend(LedgerProduce ledgerProduce, LedgerDangerSubtotal ledgerDangerSubtotal) {
        ledgerDangerSubtotal.setVldsYear(String.valueOf(DateUtils.getYear(new Date())));
        ledgerDangerSubtotal.setVldsMonth(String.valueOf(DateUtils.getMonth(new Date())));

        List<LedgerDangerSubtotal> ledgerDangerSubtotalList = ledgerDangerSubtotalMapper.selectList(ledgerDangerSubtotal);

        if (!CollectionUtils.isEmpty(ledgerDangerSubtotalList)) {
            LedgerDangerSubtotal ledgerDanger = ledgerDangerSubtotalList.get(0);
            BigDecimal scale = ledgerDanger.getVldsScale();
            ledgerDanger.setVldsScale(scale.add(ledgerProduce.getWlpNumber()));
            this.update(ledgerDanger);
        } else {
            LedgerDangerSubtotal ledgerDanger = new LedgerDangerSubtotal();
            BeanUtils.copyProperties(ledgerDangerSubtotal, ledgerDanger);
            ledgerDanger.setVldsScale(ledgerProduce.getWlpNumber());
            ledgerDangerSubtotalMapper.insert(ledgerDanger);
        }
    }

    private void ledgerDangerSubRemove(LedgerProduce ledgerProduce, LedgerDangerSubtotal ledgerDangerSubtotal) {
        ledgerDangerSubtotal.setVldsYear(String.valueOf(DateUtils.getYear(ledgerProduce.getWlpCreateTime())));
        ledgerDangerSubtotal.setVldsMonth(String.valueOf(DateUtils.getMonth(ledgerProduce.getWlpCreateTime())));

        List<LedgerDangerSubtotal> ledgerDangerSubtotalList = ledgerDangerSubtotalMapper.selectList(ledgerDangerSubtotal);

        if (!CollectionUtils.isEmpty(ledgerDangerSubtotalList)) {
            LedgerDangerSubtotal ledgerDanger = ledgerDangerSubtotalList.get(0);
            BigDecimal scale = ledgerDanger.getVldsScale();
            ledgerDanger.setVldsScale(scale.subtract(ledgerProduce.getWlpNumber()));
            this.update(ledgerDanger);
        } else {
            // 无数据时修改，数据会出现错误
            throw new ServiceException("数据错误!");
        }
    }

    private void ledgerDangerSubChange(LedgerProduce ledgerProduce, LedgerDangerSubtotal ledgerDangerSubtotal)  {
        ledgerDangerSubtotal.setVldsYear(String.valueOf(DateUtils.getYear(ledgerProduce.getWlpCreateTime())));
        ledgerDangerSubtotal.setVldsMonth(String.valueOf(DateUtils.getMonth(ledgerProduce.getWlpCreateTime())));

        LedgerProduce oldLedgerProduce = ledgerProduceMapper.selectById(ledgerProduce.getId());
        List<LedgerDangerSubtotal> ledgerDangerSubtotalList = ledgerDangerSubtotalMapper.selectList(ledgerDangerSubtotal);

        // 有历史记录，且有相关的统计数据
        if (oldLedgerProduce != null && !CollectionUtils.isEmpty(ledgerDangerSubtotalList)) {
            LedgerDangerSubtotal ledgerDanger = ledgerDangerSubtotalList.get(0);
            BigDecimal oldScale = oldLedgerProduce.getWlpNumber();
            BigDecimal scale = ledgerDanger.getVldsScale();
            // 减去旧数据，添加新数据
            ledgerDanger.setVldsScale(scale.subtract(oldScale).add(ledgerProduce.getWlpNumber()));
            this.update(ledgerDanger);
        } else {
            // 无数据时修改，数据会出现错误
            throw new ServiceException("数据错误!");
        }
    }

    private void ledgerDangerSubAppend(LedgerDispose ledgerDispose, LedgerDangerSubtotal ledgerDangerSubtotal) {
        ledgerDangerSubtotal.setVldsYear(String.valueOf(DateUtils.getYear(ledgerDispose.getWldCreateTime())));
        ledgerDangerSubtotal.setVldsMonth(String.valueOf(DateUtils.getMonth(ledgerDispose.getWldCreateTime())));

        List<LedgerDangerSubtotal> ledgerDangerSubtotalList = ledgerDangerSubtotalMapper.selectList(ledgerDangerSubtotal);

        if (!CollectionUtils.isEmpty(ledgerDangerSubtotalList)) {
            LedgerDangerSubtotal ledgerDanger = ledgerDangerSubtotalList.get(0);
            BigDecimal scale = ledgerDanger.getVldsScale();
            ledgerDanger.setVldsScale(scale.add(ledgerDispose.getWldNumber()));
            this.update(ledgerDanger);
        } else {
            LedgerDangerSubtotal ledgerDanger = new LedgerDangerSubtotal();
            BeanUtils.copyProperties(ledgerDangerSubtotal, ledgerDanger);
            ledgerDanger.setVldsScale(ledgerDispose.getWldNumber());
            ledgerDangerSubtotalMapper.insert(ledgerDanger);
        }
    }

    private void ledgerDangerSubRemove(LedgerDispose ledgerDispose, LedgerDangerSubtotal ledgerDangerSubtotal)  {
        ledgerDangerSubtotal.setVldsYear(String.valueOf(DateUtils.getYear(ledgerDispose.getWldCreateTime())));
        ledgerDangerSubtotal.setVldsMonth(String.valueOf(DateUtils.getMonth(ledgerDispose.getWldCreateTime())));

        List<LedgerDangerSubtotal> ledgerDangerSubtotalList = ledgerDangerSubtotalMapper.selectList(ledgerDangerSubtotal);

        if (!CollectionUtils.isEmpty(ledgerDangerSubtotalList)) {
            LedgerDangerSubtotal ledgerDanger = ledgerDangerSubtotalList.get(0);
            BigDecimal scale = ledgerDanger.getVldsScale();
            ledgerDanger.setVldsScale(scale.subtract(ledgerDispose.getWldNumber()));
            this.update(ledgerDanger);
        } else {
            // 无数据时移除，数据会出现错误
            throw new ServiceException("数据错误!");
        }
    }
    private void ledgerDangerSubChange(LedgerDispose ledgerDispose, LedgerDangerSubtotal ledgerDangerSubtotal)  {
        ledgerDangerSubtotal.setVldsYear(String.valueOf(DateUtils.getYear(ledgerDispose.getWldCreateTime())));
        ledgerDangerSubtotal.setVldsMonth(String.valueOf(DateUtils.getMonth(ledgerDispose.getWldCreateTime())));

        LedgerProduce oldLedgerProduce = ledgerProduceMapper.selectById(ledgerDispose.getId());
        List<LedgerDangerSubtotal> ledgerDangerSubtotalList = ledgerDangerSubtotalMapper.selectList(ledgerDangerSubtotal);

        // 有历史记录，且有相关的统计数据
        if (oldLedgerProduce != null && !CollectionUtils.isEmpty(ledgerDangerSubtotalList)) {
            LedgerDangerSubtotal ledgerDanger = ledgerDangerSubtotalList.get(0);
            BigDecimal oldScale = oldLedgerProduce.getWlpNumber();
            BigDecimal scale = ledgerDanger.getVldsScale();
            // 减去旧数据，添加新数据
            ledgerDanger.setVldsScale(scale.subtract(oldScale).add(ledgerDispose.getWldNumber()));
            this.update(ledgerDanger);
        } else {
            // 无数据时修改，数据会出现错误
            throw new ServiceException("数据错误!");
        }
    }

}
